package com.ruoyi.web.controller.byw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.UserViteaQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywUserHaveTiktokUser;
import com.ruoyi.system.service.IBywUserHaveTiktokUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的简历Controller
 * 
 * @author ruoyi
 * @date 2024-05-25
 */
@RestController
@RequestMapping("/byw/user-have-tiktok-user")
public class BywUserHaveTiktokUserController extends BaseController
{
    @Autowired
    private IBywUserHaveTiktokUserService bywUserHaveTiktokUserService;

    @PreAuthorize("@ss.hasPermi('byw:user-have-tiktok-user:list')")
    @PostMapping("/list")
    public TableDataInfo postList(@RequestBody UserViteaQuery userViteaQuery)
    {
        startPage();
        userViteaQuery.setUserId(getUserId());
        if (userViteaQuery.getUnlockTimeStart() != null){
            userViteaQuery.setUnlockTimeStart(DateUtils.getDayStartTime(userViteaQuery.getUnlockTimeStart()));
        }
        if (userViteaQuery.getUnlockTimeEnd() != null){
            userViteaQuery.setUnlockTimeEnd(DateUtils.getDayEndTime(userViteaQuery.getUnlockTimeEnd()));
        }
        if (CollectionUtils.isNotEmpty(userViteaQuery.getCityExpect())){
            userViteaQuery.setCityExpect(city(userViteaQuery.getCityExpect()));
        }

        if (CollectionUtils.isNotEmpty(userViteaQuery.getIntendedCity())){
            userViteaQuery.setIntendedCity(city(userViteaQuery.getIntendedCity()));
        }

        List<BywUserHaveTiktokUser> list = bywUserHaveTiktokUserService.selecUserTiktokUserList(userViteaQuery);
        TableDataInfo dataTable = getDataTable(list);

        BywUserHaveTiktokUser bywUserHaveTiktokUserTotal = new BywUserHaveTiktokUser();
        bywUserHaveTiktokUserTotal.setUserId(getUserId());
        List<BywUserHaveTiktokUser> bywUserHaveTiktokUsersTotal = bywUserHaveTiktokUserService.selectBywUserHaveTiktokUserList(bywUserHaveTiktokUserTotal);

        BywUserHaveTiktokUser bywUserHaveTiktokUser = new BywUserHaveTiktokUser();
        bywUserHaveTiktokUser.setUserId(getUserId());
        bywUserHaveTiktokUser.setTreatmentState(0);
        List<BywUserHaveTiktokUser> bywUserHaveTiktokUsers = bywUserHaveTiktokUserService.selectBywUserHaveTiktokUserList(bywUserHaveTiktokUser);
        dataTable.setMsg("简历总数："+bywUserHaveTiktokUsersTotal.size()+" 已处理："+(bywUserHaveTiktokUsersTotal.size() - bywUserHaveTiktokUsers.size())+" 未处理："+bywUserHaveTiktokUsers.size());
        Map<String, Integer> haveTiktokUserInfo = new HashMap<>();
        haveTiktokUserInfo.put("total",bywUserHaveTiktokUsersTotal.size());
        haveTiktokUserInfo.put("untreated",bywUserHaveTiktokUsers.size());
        dataTable.setData(haveTiktokUserInfo);
        return dataTable;
    }

    private List<String> city(List<String> cityList){
        List<String> resultList = new ArrayList<>();
        for (String city :cityList){
            if(city.endsWith("市")){
                resultList.add(city.replace("市",""));
            }
            if(city.endsWith("特别行政区")){
                resultList.add(city.replace("特别行政区",""));
            }
            if(city.endsWith("自治州")){
                resultList.add(city.replace("自治州",""));
            }
        }
        return resultList;
    }

    /**
     * 导出我的简历列表
     */
    @PreAuthorize("@ss.hasPermi('byw:user-have-tiktok-user:export')")
    @Log(title = "我的简历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserViteaQuery userViteaQuery)
    {
        userViteaQuery.setUserId(getUserId());
        List<BywUserHaveTiktokUser> list = bywUserHaveTiktokUserService.selecUserTiktokUserList(userViteaQuery);
        ExcelUtil<BywUserHaveTiktokUser> util = new ExcelUtil<BywUserHaveTiktokUser>(BywUserHaveTiktokUser.class);
        util.exportExcel(response, list, "我的简历数据");
    }
    /**
     * 处理我的简历
     */
    @PreAuthorize("@ss.hasPermi('byw:user-have-tiktok-user:treatment')")
    @Log(title = "处理我的简历", businessType = BusinessType.OTHER)
    @PutMapping("/treatment/{userPhone}")
    public AjaxResult treatment(@PathVariable("userPhone") String userPhone)
    {
        BywUserHaveTiktokUser bywUserHaveTiktokUser = new BywUserHaveTiktokUser();
        bywUserHaveTiktokUser.setUserId(getUserId());
        bywUserHaveTiktokUser.setUserPhone(userPhone);
        bywUserHaveTiktokUser.setTreatmentState(1);
        bywUserHaveTiktokUser.setUpdateBy(getUsername());
        bywUserHaveTiktokUser.setUpdateTime(DateUtils.getNowDate());
        return toAjax(bywUserHaveTiktokUserService.updateBywUserHaveTiktokUser(bywUserHaveTiktokUser));
    }
}
